﻿using System;

namespace UnitTesting.Fixturing
{
	class EnumValueProvider : IValueProvider
	{
		readonly ISimpleTypeGenerator _simpleTypeGenerator;

		internal EnumValueProvider(ISimpleTypeGenerator simpleTypeGenerator)
		{
			_simpleTypeGenerator = simpleTypeGenerator;
		}

		public object Create(Type type)
		{
			Ensure.IsNotNull(() => type);

			if (!type.IsEnum)
				return NotResolved.Value;

			var names = Enum.GetNames(type);
			int number = _simpleTypeGenerator.Next(type);
			string targetName = names[number%names.Length];
			return Enum.Parse(type, targetName);
		}
	}
}